package com.example.cardgame
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

//图片直接传递给页面，不需要依赖于文件
class test_third : AppCompatActivity() {
    private var capturedImage: Bitmap? = null
    private lateinit var takePictureLauncher: ActivityResultLauncher<Intent>

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.test_second)

        // 初始化拍摄图片的结果回调
        takePictureLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val extras = result.data?.extras
                capturedImage = extras?.get("data") as Bitmap?

                val imageView = findViewById<ImageView>(R.id.imageView)
                imageView.setImageBitmap(capturedImage)
            }
        }

        // 加载上一个页面传过来的信息
        val usernameHint = intent.getStringExtra("username") ?: ""
        val snoHint = intent.getStringExtra("sno") ?: ""
        val phoneNumberHint = intent.getStringExtra("phoneNumber") ?: ""
        val emailHint = intent.getStringExtra("email") ?: ""

        // 设置文本框的提示信息
        findViewById<EditText>(R.id.username).hint = usernameHint
        findViewById<EditText>(R.id.sno).hint = snoHint
        findViewById<EditText>(R.id.phoneNumber).hint = phoneNumberHint
        findViewById<EditText>(R.id.editTextTextEmailAddress).hint = emailHint

        val confirm: Button = findViewById(R.id.confirm)
        val buttonPhoto: ImageButton = findViewById(R.id.photo)

        // 拍摄按钮点击事件
        buttonPhoto.setOnClickListener {
            dispatchTakePictureIntent()
        }

        // 确认按钮点击事件
        confirm.setOnClickListener {
            val username = findViewById<EditText>(R.id.username).text.toString()
            val sno = findViewById<EditText>(R.id.sno).text.toString()
            val phoneNumber = findViewById<EditText>(R.id.phoneNumber).text.toString()
            val email = findViewById<EditText>(R.id.editTextTextEmailAddress).text.toString()

            // 获取性别
            val genderGroup: RadioGroup = findViewById(R.id.genderRadioGroup)
            val selectedGenderId: Int = genderGroup.checkedRadioButtonId
            val gender: String = if (selectedGenderId != -1) {
                findViewById<RadioButton>(selectedGenderId).text.toString()
            } else {
                ""
            }

            // 获取兴趣爱好
            val hobbies = arrayListOf<String>()
            if (findViewById<CheckBox>(R.id.checkBox).isChecked) hobbies.add("唱歌")
            if (findViewById<CheckBox>(R.id.checkBox2).isChecked) hobbies.add("看电影")
            if (findViewById<CheckBox>(R.id.checkBox3).isChecked) hobbies.add("打游戏")
            if (findViewById<CheckBox>(R.id.checkBox4).isChecked) hobbies.add("其他")

            // 将用户信息和图像数据通过 Intent 返回给上一页面
            val intent = Intent()
            intent.putExtra("userInfo", arrayOf(username, sno, phoneNumber, email, gender, *hobbies.toTypedArray()))
            intent.putExtra("capturedImage", capturedImage)

            setResult(6, intent)
            finish()
        }
    }

    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (takePictureIntent.resolveActivity(packageManager) != null) {

            takePictureLauncher.launch(takePictureIntent)
        }
    }
}
